package com.huluobo.a2208parkdemo.parking

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.DatePicker
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2208parkdemo.R
import com.huluobo.a2208parkdemo.adapter.CarImgAdapter
import com.huluobo.a2208parkdemo.adapter.ColorAdapter
import com.huluobo.a2208parkdemo.databinding.ActivityAddParkingBinding
import com.huluobo.base.bean.ColorBean
import com.huluobo.base.bean.ParkingBeanItem
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.util.Calendar

@AndroidEntryPoint
class AddParkingActivity : BaseActivity<ActivityAddParkingBinding, ParkingViewModel>() {
    private lateinit var parkingBeanItem: ParkingBeanItem
    private lateinit var colorAdapter: ColorAdapter
    private lateinit var carImagAdapter: CarImgAdapter
    override fun initData() {
        vm.getCarColor()
        vm.getCarColorsSuccess.observe(this) {
            val list = Gson().fromJson(it, ColorBean::class.java)
            colorAdapter.data.clear()
            colorAdapter.data.addAll(list)
            colorAdapter.notifyDataSetChanged()
        }

        vm.addParkingSuccess.observe(this) {
            ToastUtils.showLong("添加成功")
            finish()
            Log.i("TagA", "add:$it")
        }
    }

    override fun initView() {
        parkingBeanItem = intent.getSerializableExtra(Const.PARAM_PARKING_BEAN) as ParkingBeanItem

        binding.addParkingHeaer.headerTitle.text = "添加车位"
        binding.addParkingHeaer.headerBack.setOnClickListener { finish() }

        binding.parkingStartTimeNowTv.text = parkingBeanItem.parkStart
        binding.parkingEndTimeNowTv.text = parkingBeanItem.parkEnd
        binding.parkingTimeNowTv.text = parkingBeanItem.parkTime
        binding.parkingCarNameNowTv.text = parkingBeanItem.parkName

        binding.parkingColorRv.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        colorAdapter = ColorAdapter()
        binding.parkingColorRv.adapter = colorAdapter

        colorAdapter.setOnItemClickListener { adapter, view, position ->
            binding.parkingCarColorSelectIv.setBackgroundColor(Color.parseColor(colorAdapter.data[position].colorValue))
        }

        binding.parkingStartTimeIv.setOnClickListener {
            showDateDialog(binding.parkingStartTimeNowTv)
        }

        binding.parkingEndTimeIv.setOnClickListener {
            showDateDialog(binding.parkingEndTimeNowTv)
        }

        binding.parkingCarImgRv.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        carImagAdapter = CarImgAdapter()
        binding.parkingCarImgRv.adapter = carImagAdapter

        carImagAdapter.data.add("+")
        carImagAdapter.notifyDataSetChanged()

        carImagAdapter.setOnItemClickListener { adapter, view, position ->
            if (position == adapter.data.size - 1) {
                PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()
            }
        }

        binding.parkingCommitBtn.setOnClickListener {
            if (binding.parkingEndTimeNowTv.text.isNotEmpty() &&
                binding.parkingStartTimeNowTv.text.isNotEmpty() &&
                SPUtils.getInstance().getString(Const.PARAM_USERNAME).isNotEmpty() &&
                SPUtils.getInstance().getInt(Const.PARAM_UID) != -1
            ) {
                val map = mutableMapOf<String, Any>()
                map[Const.PARAM_PARK_START] = binding.parkingStartTimeNowTv.text.toString()
                map[Const.PARAM_PARK_END] = binding.parkingEndTimeNowTv.text.toString()
                map[Const.PARAM_PARK_NAME] = SPUtils.getInstance().getString(Const.PARAM_USERNAME)
                map[Const.PARAM_PARK_USER] = SPUtils.getInstance().getInt(Const.PARAM_UID)
                map[Const.PARAM_PARK_TIME] = getTime()

                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                vm.addParking(body)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val bundle = data?.extras
            val bean = bundle?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            val filePath = bean?.path
            if (filePath != null) {
                carImagAdapter.data.add(0, filePath)
                carImagAdapter.notifyDataSetChanged()
            }
        }
    }

    private fun showDateDialog(textView: TextView) {
        val calendar = Calendar.getInstance()
        DatePickerDialog(
            this,
            { view, year, month, dayOfMonth ->
                textView.text = "$year-${month + 1}-$dayOfMonth"
            }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)
        ).show()

    }

}